import request from '@/api/request'
import { buildQueryString } from '@/utils/request'

/**
 * 维修工单工作管理API
 */
export const repairWorkApi = {
  /**
   * 分页查询维修工单（工作管理扩展）
   * @param {Object} params 查询参数
   */
  getRepairOrderWorkPage(params) {
    return request.get('/api/property/repair-order-work/page', params)
  },

  /**
   * 根据ID查询维修工单详情（工作管理扩展）
   * @param {Number} id 工单ID
   */
  getRepairOrderWorkById(id) {
    return request.get(`/api/property/repair-order-work/${id}`)
  },

  /**
   * 新增维修工单（工作管理扩展）
   * @param {Object} data 工单数据
   */
  addRepairOrderWork(data) {
    return request.post('/api/property/repair-order-work', data)
  },

  /**
   * 更新维修工单（工作管理扩展）
   * @param {Object} data 工单数据
   */
  updateRepairOrderWork(data) {
    return request.put('/api/property/repair-order-work', data)
  },

  /**
   * 分配维修工单
   * @param {Number} orderId 工单ID
   * @param {Number} repairmanId 维修人员ID
   * @param {String} remark 备注
   */
  assignRepairOrder(orderId, repairmanId, remark) {
    // 构建查询参数
    const queryParams = buildQueryString({
      repairmanId,
      remark
    })
    
    // 构建完整的URL
    const url = `/api/property/repair-orders/${orderId}/assign${queryParams ? '?' + queryParams : ''}`
    
    return request.put(url, {})
  },

  /**
   * 批量分配维修工单
   * @param {Array} orderIds 工单ID列表
   * @param {Number} repairmanId 维修人员ID
   * @param {String} estimatedCompleteTime 预计完成时间
   */
  batchAssignRepairOrders(orderIds, repairmanId, estimatedCompleteTime) {
    return request.post('/api/property/repair-order-work/batch-assign', {
      orderIds,
      repairmanId,
      estimatedCompleteTime
    })
  },

  /**
   * 开始维修
   * @param {Number} orderId 工单ID
   * @param {Array} materialList 材料清单
   * @param {String} remark 备注
   */
  startRepair(orderId, materialList, remark) {
    return request.post('/api/property/repair-order-work/start', {
      orderId,
      materialList,
      remark
    })
  },

  /**
   * 完成维修
   * @param {Number} orderId 工单ID
   * @param {String} repairResult 维修结果
   * @param {String} repairCost 维修费用
   * @param {String} workHours 工时
   * @param {String} remark 备注
   */
  completeRepair(orderId, repairResult, repairCost, workHours, remark) {
    return request.post('/api/property/repair-order-work/complete', {
      orderId,
      repairResult,
      repairCost,
      workHours,
      remark
    })
  },

  /**
   * 提交客户评价
   * @param {Number} orderId 工单ID
   * @param {Number} satisfaction 满意度评分
   * @param {String} comment 评价内容
   */
  submitCustomerRating(orderId, satisfaction, comment) {
    return request.post('/api/property/repair-order-work/rating', {
      orderId,
      satisfaction,
      comment
    })
  },

  /**
   * 查询维修人员工单
   * @param {Number} repairmanId 维修人员ID
   * @param {Number} status 状态
   */
  getRepairOrdersByRepairman(repairmanId, status) {
    return request.get('/api/property/repair-order-work/repairman', {
      repairmanId,
      status
    })
  },

  /**
   * 查询紧急工单
   */
  getUrgentRepairOrders() {
    return request.get('/api/property/repair-order-work/urgent')
  },

  /**
   * 查询超时工单
   * @param {Number} timeoutHours 超时小时数
   */
  getTimeoutRepairOrders(timeoutHours) {
    return request.get('/api/property/repair-order-work/timeout', {
      timeoutHours
    })
  },

  /**
   * 查询待分配工单
   */
  getPendingAssignRepairOrders() {
    return request.get('/api/property/repair-order-work/pending-assign')
  },

  /**
   * 查询进行中工单
   */
  getInProgressRepairOrders() {
    return request.get('/api/property/repair-order-work/in-progress')
  },

  /**
   * 查询已完成工单
   * @param {String} startDate 开始日期
   * @param {String} endDate 结束日期
   */
  getCompletedRepairOrders(startDate, endDate) {
    return request.get('/api/property/repair-order-work/completed', {
      startDate,
      endDate
    })
  },

  /**
   * 查询维修工单统计信息
   * @param {String} startDate 开始日期
   * @param {String} endDate 结束日期
   */
  getRepairOrderWorkStatistics(startDate, endDate) {
    return request.get('/api/property/repair-order-work/statistics', {
      startDate,
      endDate
    })
  },

  /**
   * 查询维修人员工作量统计
   * @param {Number} repairmanId 维修人员ID
   * @param {String} startDate 开始日期
   * @param {String} endDate 结束日期
   */
  getRepairmanWorkloadStatistics(repairmanId, startDate, endDate) {
    return request.get('/api/property/repair-order-work/repairman-workload', {
      repairmanId,
      startDate,
      endDate
    })
  },

  /**
   * 查询工单类型统计
   * @param {String} startDate 开始日期
   * @param {String} endDate 结束日期
   */
  getRepairOrderTypeStatistics(startDate, endDate) {
    return request.get('/api/property/repair-order-work/type-statistics', {
      startDate,
      endDate
    })
  },

  /**
   * 查询客户满意度统计
   * @param {String} startDate 开始日期
   * @param {String} endDate 结束日期
   */
  getCustomerSatisfactionStatistics(startDate, endDate) {
    return request.get('/api/property/repair-order-work/satisfaction-statistics', {
      startDate,
      endDate
    })
  },

  /**
   * 设置回访
   * @param {Number} orderId 工单ID
   * @param {String} followUpTime 回访时间
   * @param {String} remark 备注
   */
  setFollowUp(orderId, followUpTime, remark) {
    return request.post('/api/property/repair-order-work/follow-up', {
      orderId,
      followUpTime,
      remark
    })
  },

  /**
   * 完成回访
   * @param {Number} orderId 工单ID
   * @param {String} followUpResult 回访结果
   */
  completeFollowUp(orderId, followUpResult) {
    return request.post('/api/property/repair-order-work/follow-up-complete', {
      orderId,
      followUpResult
    })
  },

  /**
   * 通知客户
   * @param {Number} orderId 工单ID
   * @param {Number} notificationMethod 通知方式
   * @param {String} content 通知内容
   */
  notifyCustomer(orderId, notificationMethod, content) {
    return request.post('/api/property/repair-order-work/notify', {
      orderId,
      notificationMethod,
      content
    })
  },

  /**
   * 获取工单来源列表
   */
  getRepairOrderSourceList() {
    return request.get('/api/property/repair-order-work/sources')
  },

  /**
   * 获取工单分类列表
   */
  getRepairOrderCategoryList() {
    return request.get('/api/property/repair-order-work/categories')
  },

  /**
   * 获取通知方式列表
   */
  getNotificationMethodList() {
    return request.get('/api/property/repair-order-work/notification-methods')
  },

  // ========================= 移动端维修管理API =========================

  /**
   * 获取维修工单统计概览（移动端）
   */
  getMobileStatistics() {
    return request.get('/api/property/repair-orders/mobile/statistics')
  },

  /**
   * 获取待分配工单列表（移动端）
   * @param {Number} current 当前页
   * @param {Number} size 每页大小
   */
  getMobilePendingAssignmentOrders(current = 1, size = 10) {
    return request.get('/api/property/repair-orders/mobile/pending-assignment', {
      current,
      size
    })
  },

  /**
   * 获取我的工单列表（移动端）
   * @param {Number} current 当前页
   * @param {Number} size 每页大小
   * @param {Number} status 工单状态（可选）
   */
  getMobileMyOrders(current = 1, size = 10, status = null) {
    return request.get('/api/property/repair-orders/mobile/my-assigned-orders', {
      current,
      size,
      status
    })
  },

  /**
   * 获取用户数据权限信息（移动端）
   */
  getMobileUserPermissionInfo() {
    return request.get('/api/property/repair-orders/mobile/user-permission-info')
  },

  /**
   * 获取维修工单详情（移动端）
   * @param {Number} id 工单ID
   */
  getRepairDetail(id) {
    return request.get(`/api/property/repair-orders/mobile/${id}/detail`)
  },

  /**
   * 完成维修工单（移动端）
   * @param {Number} id 工单ID
   */
  completeRepair(id) {
    return request.post(`/api/property/repair-orders/mobile/${id}/complete`)
  },

  /**
   * 受理维修工单（移动端）
   * @param {Number} id 工单ID
   * @param {String} remark 受理备注（可选）
   */
  acceptRepair(id, remark = '') {
    return request.post(`/api/property/repair-orders/mobile/${id}/accept`, {
      remark
    })
  },

  /**
   * 获取维修人员列表
   * @returns {Promise} 维修人员列表
   */
  getRepairmenList() {
    return request.get('/api/property/repairmen')
  }
}
